package com.bookRecommend.service.impl;

import com.bookRecommend.mapper.IBookMapper;
import com.bookRecommend.model.Book;
import com.bookRecommend.service.IBookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements IBookService {
    @Autowired
    IBookMapper mapper;

    /*
     * 查询图书信息
     */
    @Override
    public List<Book> findAllBooks() {
        return mapper.selectAllBooks();
    }

    /*
     * 查找图书
     * */
    public List<Book> findByBook(String name){
        Book book = new Book();
        if (name == null){
            return null;
        }
        book.setBookName(name);
        return mapper.selectByBook(book);

    }

    @Override
    public Book findByBookId(Integer id) {
        if (id == null) {
            return null;
        }
        Book book = new Book();
        book.setId(id);
        return mapper.selectByBook(book).get(0);
    }

    /**
     * 图书新增
     */
    @Override
    public int addBook(Book book) {
        if (book == null){
            return 0;
        }
        return mapper.insertBook(book);
    }

    /**
     * 图书修改
     */

    @Override
    public int modifyBook(Book book) {
        if (book == null){
            return 0;
        }
        return mapper.updateBook(book);
    }
    /**
     * 删除图书
     */
    @Override
    public int removeBook(Book book){
        if (book == null){
            return 0;
        }
        return mapper.deleteBook(book);
    }

    @Override
    public PageInfo<Book> findPage(Integer n, Integer size, String bookName, Integer typeId) {
        if (n == null) {
            n = 1;
        }
        if (size == null) {
            size = 5;
        }
        PageHelper.startPage(n, size);
        if (bookName != null && bookName.length() > 0) {
            Book book = new Book();
            book.setBookName(bookName);
            return new PageInfo<>(mapper.selectByBook(book));
        }
        if (typeId !=null) {
            Book book = new Book();
            book.setTypeId(typeId);
            return new PageInfo<>(mapper.selectByBook(book));
        }
        return new PageInfo<>(mapper.selectPage());
    }

    @Override
    public int modifyByBook(Book book) {
        return mapper.updateBook(book);
    }

    @Override
    public List<Book> getHot() {
        return mapper.selectHot();
    }

    @Override
    public List<Book> getHotScore() {
        return mapper.selectHotScore();
    }
}
